package com.sxkf.template.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sxkf.core.third.im.entity.ExtensionJsonEntity;
import com.sxkf.core.third.im.entity.UpdateUser;
import com.sxkf.core.third.im.service.YxService;
import com.sxkf.system.entity.SysUser;
import com.sxkf.system.mapper.SysUserMapper;
import com.sxkf.system.vo.PassStoreAuditVO;
import com.sxkf.template.constants.OrderConstant;
import com.sxkf.template.dto.SettledOrderDto;
import com.sxkf.template.entity.*;
import com.sxkf.template.mapper.SettledRecordMapper;
import com.sxkf.template.mapper.StoreMapper;
import com.sxkf.template.service.OrderService;
import com.sxkf.template.service.SettledRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxkf.template.service.UserService;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.CheckEmptyUtil;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-05 16:15:45
 */
@Service
public class SettledRecordServiceImpl extends ServiceImpl<SettledRecordMapper, SettledRecord> implements SettledRecordService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private YxService yxService;
    @Resource
    private UserService userService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pass(PassStoreAuditVO passStoreAuditVO) {
        SettledRecord settledRecord = getById(passStoreAuditVO.getSettledId());
        if (settledRecord.getState().equals("2")){
            return;
        }
        settledRecord.setState("2");
        updateById(settledRecord);

        Store store = new Store();
        BeanUtils.copyProperties(passStoreAuditVO,store);
        // 经纬度从 PassStoreAuditVO 变动到 SettledRecord
        store.setUserId(settledRecord.getUserId());
        store.setLatitude(settledRecord.getLatitude());
        store.setLongitude(settledRecord.getLongitude());
        store.setCategoryId(passStoreAuditVO.getCategoryId());
        store.setName(settledRecord.getName());
        store.setType(settledRecord.getType());
        store.setPhone(settledRecord.getContactPhone());
        store.setStatus("0");
        store.setIsAuth("1");
        store.setOpeningDate(new Date());
        store.setFansCount(0);
        store.setAmount(BigDecimal.ZERO);
        storeMapper.insert(store);

        String token = yxService.register(store.getId());
        SysUser sysUser = new SysUser();
        sysUser.setPhone(settledRecord.getContactPhone());
        sysUser.setImToken(token);
        sysUser.setStatus(1);
        sysUser.setStoreId(store.getId());
        String salt = oConvertUtils.randomGen(8);
        sysUser.setSalt(salt);
        sysUser.setUsername(passStoreAuditVO.getUsername());
        String passwordEncode = PasswordUtil.encrypt(sysUser.getPassword(), sysUser.getUsername(), salt);
        sysUser.setPassword(passwordEncode);
        sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
        sysUserMapper.insert(sysUser);

        UpdateUser updateUser = new UpdateUser();
        updateUser.setAvatar(sysUser.getAvatar());
        updateUser.setName(sysUser.getUsername());
        ExtensionJsonEntity extensionJsonEntity = new ExtensionJsonEntity();
        extensionJsonEntity.setType("2");
        String extension = JSONUtil.toJsonStr(extensionJsonEntity);
        updateUser.setExtension(extension);
        yxService.updateUser(store.getId(), updateUser);

        update(new LambdaUpdateWrapper<SettledRecord>()
                .set(SettledRecord::getSysUserId,sysUser.getId())
                .eq(SettledRecord::getId,passStoreAuditVO.getSettledId())
        );
    }

    @Override
    public void reject(String id, String reason) {
        update(new LambdaUpdateWrapper<SettledRecord>()
                .set(SettledRecord::getState,"3")
                .set(SettledRecord::getReason,reason)
                .eq(SettledRecord::getId,id));
    }


    public Boolean paySuccess(SettledOrderDto order){
        boolean update = update(new LambdaUpdateWrapper<SettledRecord>()
                .set(SettledRecord::getIsPay, "1")
                .eq(SettledRecord::getId, order.getOrderNo()));
        return update;

    }


}
